package com.gerald.shop.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Gerald 2022/1/6 18:01
 * description:
 */
@Configuration
public class ElasticAutoConfig {

    @Value("${es.host}")
    private String host;

    @Value("${es.port}")
    private Integer port;

    @Bean
    public RestHighLevelClient restHighLevelClient() {

        // 设连接的host和端口
        HttpHost httpHost = new HttpHost(host,port);

        // 创建RestClientBuilder
        RestClientBuilder builder = RestClient.builder(httpHost);

        // 创建RestHighLevelClient
        RestHighLevelClient client = new RestHighLevelClient(builder);

        // 返回
        return client;
    }
}
